package com.itcast.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcast.pojo.Address;
import com.itcast.pojo.User;
import com.itcast.service.Addressservice;
import com.itcast.utils.Respone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-19
 */
@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private Addressservice addressservice;

    @GetMapping("/list")
    public Respone list(@RequestParam("page") int page, @RequestParam("limit") int limit,@RequestParam("userId")Integer id){
        List<Integer> ids = addressservice.addressList(id);
        QueryWrapper<Address> queryWrapper=new QueryWrapper<>();
        queryWrapper.ge("aid",ids);
        Page<Address> pageSize=new Page<>(page,limit);
        IPage<Address> page1 = addressservice.page(pageSize, queryWrapper);
        return new Respone(0,"成功",page1.getRecords(),(int)page1.getTotal());
    }

    @PostMapping("/update")
    public Respone update(@RequestBody Address address){
        address.setUpDates(new Date());
        System.out.println(address.getUpDates());
        boolean update = addressservice.updateById(address);
        if(update){
            return new Respone(0,"更新成功");
        }
        return new Respone(999,"更新失败");
    }

    @GetMapping("/delete")
    public Respone del(@RequestParam("aid") int aid){
        if(  addressservice.removeById(aid)){
            return new Respone(0,"删除成功");
        }
        return new Respone(999,"删除失败");
    }

}

